package com.sn.author.common.sdk.util;

import java.util.Date;
import java.util.Optional;
import java.util.Random;

/**
 * @Author xiaom
 * @Date 2020/6/28 11:21
 * @Version 1.0.0
 * @Description <>
 **/
public class Utils {

    public static <T> boolean isEmpty(T arg) {
        try {
            return Optional.of(arg)
                    .map(Object::toString)
                    .map(String::trim)
                    .map(c -> c.equals("null"))
                    .map(b -> b || arg.equals(""))
                    .map(a -> a || arg.equals("undefined"))
                    .orElse(false);
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * 获取精确到秒的时间戳
     *
     * @return
     */
    public static Long getSecondTimestamp(Date date) {
        if (null == date) {
            return 0L;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Long.valueOf(timestamp.substring(0, length - 3));
        } else {
            return 0L;
        }
    }

    /**
     * 生成指定位数的随机数
     *
     * @param length
     *
     * @return
     */
    public static String getRandom(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val.append(random.nextInt(10));
        }
        return val.toString();
    }

}
